﻿using System;
using Framework;
using OrderModule;
using RecipeModule;
using UnityEngine;
using UnityEngine.UI;

namespace View
{
    public class RecipeInfoView : ViewProxy
    {
        private GameObject[] _bgs;
        private RecipeDishUnit _recipeDishUnit;

        private ClickTrigger _triggerUse;
        private ClickTrigger _triggerEdit;
        private ClickTrigger _triggerDelete;
        private ClickTrigger _triggerClose;

        private Text _textCalories;
        private Text _textMoney;
        private Text _textName;

        private int _recipeId = -1;
        private Recipe _recipe;
        
        public void SetRecipeId(int recipeId)
        {
            _recipeId = recipeId;
            Update();
        }
        
        protected override void SetupView()
        {
            LoadRes("Views/Recipe/RecipeInfoView");
        }

        protected override void OnInit()
        {
            _bgs = new GameObject[3];
            for (int i = 0; i < 3; ++i)
            {
                GameObject bg = _transform.Find("Bgs/Bg_" + (i + 1)).gameObject;
                _bgs[i] = bg;
            }
            
            _recipeDishUnit = new RecipeDishUnit(_transform.Find("Dishes").gameObject);
            
            _triggerClose = ClickTrigger.Get(_transform.Find("Button_Close"));
            _triggerClose.AddClickListener(OnCloseClick);
            _triggerEdit = ClickTrigger.Get(_transform.Find("Button_Edit"));
            _triggerEdit.AddClickListener(OnEditClick);
            _triggerDelete = ClickTrigger.Get(_transform.Find("Button_Delete"));
            _triggerDelete.AddClickListener(OnDeleteClick);
            _triggerUse = ClickTrigger.Get(_transform.Find("Button_Use"));
            _triggerUse.AddClickListener(OnUseClick);

            _textCalories = _transform.Find("Text_Calories").GetComponent<Text>();
            _textMoney = _transform.Find("Text_Money").GetComponent<Text>();
            _textName = _transform.Find("Text_Name").GetComponent<Text>();

            Update();
            RecipeManager.instance.RecipeUpdateEvent += Update;
        }

        protected override void OnDispose()
        {
            RecipeManager.instance.RecipeUpdateEvent -= Update;
            if (_recipeDishUnit != null)
            {
                _recipeDishUnit.Dispose();
                _recipeDishUnit = null;
            }
        }

        protected void Update()
        {
            _textCalories.text = "0";
            _textMoney.text = "0";
            _textName.text = string.Empty;
            if (_recipeId > 0)
            {
                _recipe = RecipeManager.instance.GetRecipe(_recipeId);
            }
            if (_recipe != null)
            {
                for (var i = 0; i < _bgs.Length; i++)
                {
                    _bgs[i].SetActive(i == (int)_recipe.recipeType);
                }
                
                _textName.text = _recipe.name;
                _recipeDishUnit.Update(_recipe.listDish);
                float calories = 0f;
                float money = 0f;
                for (var i1 = 0; i1 < _recipe.listDish.Count; i1++)
                {
                    RecipeDish recipeDish = _recipe.listDish[i1];
                    Dish dish = DishConfig.GetDish(recipeDish.dishId);
                    calories += dish.calories * recipeDish.count;
                    money += dish.price * recipeDish.count;
                }
                _textCalories.text = calories.ToString();
                _textMoney.text = money.ToString();
            }
            else
            {
                _recipeDishUnit.Update(null);
            }
        }

        private void OnCloseClick(params object[] args)
        {
            Close();
        }
        
        private void OnEditClick(params object[] args)
        {
            EditRecipeView editRecipeView = new EditRecipeView();
            editRecipeView.SetRecipeId(_recipe.id);
            editRecipeView.Show();
        }
        
        private void OnDeleteClick(params object[] args)
        {
            RecipeManager.instance.RemoveRecipeById(_recipeId);
            Close();
        }
        
        private void OnUseClick(params object[] args)
        {
            _recipe = RecipeManager.instance.CloneRecipe(_recipeId);
            if (_recipe.listDish.Count > 0)
            {
                OrderManager.instance.AddOrder(_recipe);
                OrderSuccessView successView = new OrderSuccessView();
                successView.SetConfirmCallback(Close);
                successView.Show();
            }
        }
    }
}